
package com.qf.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.qf.dao.UserDao;
import com.qf.model.Contact;
import com.qf.model.TelBook;
import com.qf.model.User;
import com.qf.utils.C3P0Util;

public class UserDaoImpl implements UserDao {
	@Override
	public User findByCondition(String sql, Object[] objects) {
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		User user=null;
		try {
			user=qr.query(sql, new BeanHandler<>(User.class),objects);
		} catch (SQLException e) {
			e.printStackTrace();
				
		}
		//TODO
		return user;
			
	}
	@Override
	public List<TelBook> findAllTelbook(String sql, int id) {
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		List<TelBook> telbs=null;
		try {
			telbs=qr.query(sql, new BeanListHandler<>(TelBook.class),id);
		} catch (SQLException e) {
			e.printStackTrace();
				
		}
		//TODO
		return telbs;
			
	}
	@Override
	public List<Contact> findContacts(String sql, Object[] objects) {
		
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		List<Contact> cs=null;
		try {
			cs=qr.query(sql, new BeanListHandler<>(Contact.class),objects);
		} catch (SQLException e) {
			e.printStackTrace();
				
		}
		//TODO
		return cs;
			
	}
	@Override
	public boolean deleteContact(String sql, int cid) {
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		int i=0;
		try {
			i=qr.update(sql,cid);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		if(i==1){
			return true;
		}
		return false;
			
	}

}

	